<?php
/**
 * JBlog v1.5 To JBlog v2 升级程序
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id$
*/

require 'include/common.php';

$old_tablepre = 'jblog_';

function otable($table) {
	return $GLOBALS['old_tablepre'].$table;
}

//转换用户
$query = $db->query("SELECT * FROM ".otable('user')." ORDER BY id");
while ( $row = $db->fetch_array($query) ) {
	$row = add_slashes($row);
	$insert = array(
		'id' => $row['id'],
		'username' => $row['username'],
		'email' => $row['email'],
		'password' => $row['password'],
		'group' => $row['isfounder'] ? 'admin' : 'user',
		'url' => $row['homepage'],
		'sex' => $row['sex'],
		'lastlogin' => $row['lastlogin'],
		'regtime' => $row['regtime'],
		'logincount' => $row['logincount'],
		'lastip' => $row['ip'],
		'ischeck' => $row['ischeck'],
	);
	$db->insert('user', $insert, true);
}
$db->free_result($query);
echo '用户转换完成<br />';

//转换分类
$query = $db->query("SELECT * FROM ".otable('category')." ORDER BY id");
while ( $row = $db->fetch_array($query) ) {
	$row = add_slashes($row);
	$insert = array(
		'id' => $row['id'],
		'catename' => $row['catename'],
		'type' => 'blog',
		'count' => $row['count'],
		'orderid' => $row['orderid']
	);
	$db->insert('cate', $insert, true);	
	$insert_metas = array(
		'alias' => $row['urlname'],
		'keywords' => $row['keywords'],
		'description' => $row['description'],
		'redirect' => $row['redirect'],
	);
	foreach ( $insert_metas as $key => $value ) {
		$meta = array(
			'table' => 'cate',
			'dataid' => $row['id'],
			'var' => $key,
			'value' => $value,
		);
		$db->insert('meta', $meta);
	}
}
$db->free_result($query);
echo '日志分类导入完成。<br />';

//导入标签
$query = $db->query("SELECT * FROM ".otable('tag')." ORDER BY id");
while ( $row = $db->fetch_array($query) ) {
	$row = add_slashes($row);
	$insert = array(
		'id' => $row['id'],
		'tagname' => $row['tagname'],
		'count' => $row['count'],
	);
	$db->insert('tag', $insert, true);
}
$db->free_result($query);
echo '标签导入完成。<br />';

//导入文章
$query = $db->query("SELECT * FROM ".otable('article')." ORDER BY id ");
while ( $row = $db->fetch_array($query) ) {
	$row = add_slashes($row);
	$tags = array();
	if ( !empty($row['tags']) ) {
		$rs = $db->query("SELECT * FROM ".tname('tag')." WHERE id IN ({$row[tags]})");
		while ( $tag = $db->fetch_array($rs) ) {
			$tags[] = addslashes($tag['tagname']);
			$db->query("INSERT INTO ".tname('tagpost')."(tagid, postid) VALUES ({$tag[id]},{$row[id]})");
		}
	}
	$insert = array(
		'id' => $row['id'],
		'cateid' => $row['cid'],
		'userid' => $row['uid'],
		'username' => $row['username'],
		'title' => $row['title'],
		'alias' => $row['urlname'],
		'password' => $row['password'],
		'excerpt' => $row['excerpt'],
		'content' => $row['content'],
		'tags' => implode(',',$tags),
		'dateline' => $row['dateline'],
		'comments' => $row['comments'],
		'views' => $row['views'],
		'type' => 'blog',
		'status' => ($row['status']==1&&$row['ischeck']) ? 1 : 0,
		'commentstatus' => 1,
	);
	$db->insert('post', $insert, true);
}
$db->free_result($query);
echo '日志导入完成。<br />';

//导入页面
$query = $db->query("SELECT * FROM ".otable('page')." ORDER BY id ");
while ( $row = $db->fetch_array($query) ) {
	$row = add_slashes($row);
	$insert = array(
		'id' => $row['id'],
		'cateid' => 0,
		'userid' => $row['uid'],
		'username' => $row['username'],
		'title' => $row['title'],
		'alias' => $row['urlname'],
		'content' => $row['content'],
		'dateline' => $row['dateline'],
		'type' => 'page',
		'status' => $row['ishide'] ? 0 : 1,
		'commentstatus' => 1,
	);
	$db->insert('post', $insert, true);
}
$db->free_result($query);
echo '页面导入完成。<br />';

//导入评论
$query = $db->query("SELECT * FROM ".otable('comment')." ORDER BY id");
while ( $row = $db->fetch_array($query) ) {
	$row = add_slashes($row);
	$insert = array(
		'postid' => $row['aid'],
		'userid' => $row['uid'],
		'author' => $row['username'],
		'email' => $row['email'],
		'url' => $row['homepage'],
		'content' => $row['content'],
		'dateline' => $row['dateline'],
		'ip' => $row['ip'],
		'status' => $row['ischeck'],
	);
	$db->insert('comment', $insert);
}
$db->free_result($query);
echo '评论导入完成。<br />';

//转换链接分类
$map = array();
$query = $db->query("SELECT * FROM ".otable('linkgroup')." ORDER BY id");
while ( $row = $db->fetch_array($query) ) {
	$row = add_slashes($row);
	$insert = array(
		'catename' => $row['groupname'],
		'type' => 'link',
		'count' => 0,
		'orderid' => $row['orderid']
	);
	$newid = $db->insert('cate', $insert);
	$map[$row['id']] = $newid;	
}
$db->free_result($query);
echo '链接分类导入完成。<br />';

$query = $db->query("SELECT * FROM ".otable('link')." ORDER BY id");
while ( $row = $db->fetch_array($query) ) {
	$row = add_slashes($row);
	$insert = array(
		'cateid' => $map[$row['groupid']],
		'name' => $row['name'],
		'url' => $row['url'],
		'logo' => $row['logo'],
		'description' => $row['description'],
		'status' => 1,
		'orderid' => $row['orderid'], 
	);
	$db->insert('link', $insert);
}
$db->free_result($query);
echo '链接导入完成。<br />';

//导入附件
$query = $db->query("SELECT * FROM ".otable('upload')." ORDER BY id");
while ( $row = $db->fetch_array($query) ) {
	$row = add_slashes($row);
	$insert = array(
		'id' => $row['id'],
		'postid' => $row['aid'],
		'userid' => $row['uid'],
		'username' => $row['username'],
		'originalname' => $row['originalname'],
		'filepath' => $row['filepath'],
		'filesize' => $row['filesize'],
		'filetype' => $row['filetype'],
		'fileext' => $row['fileext'],
		'dateline' => $row['dateline'],
		'downloads' => $row['downloads'],
	);
	$db->insert('attach', $insert, true);
}
$db->free_result($query);
echo '链接导入完成。<br />';

//更新用户信息
$query = $db->query("SELECT id FROM ".tname('user')."");
while ( $row = $db->fetch_array($query) ) {
	$updata = array(
		'posts' => $db->count('post', 'userid='.$row['id']),
		'comments' => $db->count('comment', 'userid='.$row['id'])
	);
	update_user($row['id'], $updata);
}
echo '用户信息更新完成。<br />';
?>